/**
 * 站点图层类
 */
Subwaymap.StationLayer = org.gridsofts.util.Class.define({
    name: "Subwaymap.StationLayer",

    /**
     * 构造函数
     */
    construct: function (container, line, stopsOnRoute) {
        if (container == null || stopsOnRoute == null) {
            throw new Error("Object construct parameter is empty, unable to initialize instance of LineLayer.");
        }

        this.container = container;
        this.line = line;
        this.stopsOnRoute = stopsOnRoute;

        this.layer = new Kinetic.Layer({
            id: "stationlayer",
            line: line
        });
        this.container.add(this.layer);
        this.repaint();
    },

    methods: {

        /**
         * 绘画 站点
         * @param {any} layer   画布容器
         * @param {any} station 站点 
         * @param {any} group   组
         */
        drawStation: function (layer, line, station, group) {
            // 更新地图实际尺寸
            new Subwaymap.Station(layer, line, station, group);
            if (station.stopX > Subwaymap.actualWidth) {
                Subwaymap.actualWidth = station.stopX;
            }
            if (station.stopX < Subwaymap.minWidth) {
                Subwaymap.minWidth = station.stopX;
            }
            if (station.stopY > Subwaymap.actualHeight) {
                Subwaymap.actualHeight = station.stopY;
            }
            if (station.stopY < Subwaymap.minHeight) {
                Subwaymap.minHeight = station.stopY;
            }
        },

        /**
         * 绘画站点当前所属站名称  (包含中/英 名称)
         * @param {any} layer
         * @param {any} station
         * @param {any} group
         */
        drawStationName: function (layer, station, group) {
            //绘制站点名称
            new Subwaymap.StationName(layer, station, group);
        },

        /**
         * 绘画站点线路名称  例如  1 号线  2 号线
         * @param {any} layer
         * @param {any} station
         * @param {any} group
         */
        drawStationCode: function (layer, line, station, group) {
            //绘制站点线路routeMapStopID
            new Subwaymap.StationCode(layer, line, station, group);
        },

        /**
         * 绘画 Bus 位置
         * @param {any} layer
         * @param {any} station
         */
        drawBusLines: function (layer, line, station) {
            //绘画 Bus 位置
            new Subwaymap.BusLines(layer, line, station);
        },

        /**
         * 根据路网图数据，重绘路网线路
         */
        repaint: function () {
            this.layer.clear();

            this.stopsOnRoute = arguments[1] ?? this.stopsOnRoute;
            this.line = arguments[0] ?? this.line;
            // 绘制站点
            if (this.stopsOnRoute && this.stopsOnRoute.length > 0) {
                for (var i = 0; i < this.stopsOnRoute.length; i++) {
                    var station = this.stopsOnRoute[i];
                    //创建  Group，将 多个 属性放入 Group 中
                    var group = new Kinetic.Group({
                        draggable: true,
                        routeMapStopID: station.routeMapStopID,
                        routeMapRDVID: this.line.routeMapRDVID,
                        lineName: this.line.displayName,
                    });

                    // 拖拽 站点，更新最新位置
                    group.on('dragend', function () {
                        var position = this.getPosition(); // 获取当前位置
                        var currentStation = this.children[0].attrs;
                        var positionX = parseInt(position.x);
                        var positionY = parseInt(position.y);
                        var routeMapRDVID = this.attrs.routeMapRDVID;
                        var lineName = this.attrs.lineName;
                        var routeMapStopID = this.attrs.routeMapStopID;
                        var oldLine = currentStation.x + " " + currentStation.y;
                        updateDraggerStationPosition(lineName, routeMapStopID, currentStation.x + positionX, currentStation.y + positionY, oldLine);
                    });

                    this.drawStation(this.layer, this.line, station, group);
                    this.drawStationCode(this.layer, this.line, station, group);
                    this.drawStationName(this.layer, station, group);

                    //this.drawBusLines(this.layer, this.line, station);
                }
            }
        }
    }
});

